package com.scoremg.api;


import java.math.BigDecimal;
import java.util.List;

import com.scoremg.annotation.GlobalInterceptor;
import com.scoremg.entity.dto.WebSessionUserDto;
import com.scoremg.entity.enums.RoleTypeEnums;
import com.scoremg.entity.po.CourseSelectInfo;

import com.scoremg.entity.po.UserInfo;
import com.scoremg.entity.query.CourseSelectInfoQuery;


import com.scoremg.entity.query.LayuiSearchQuery;
import com.scoremg.entity.vo.LayuiResultVO;
import com.scoremg.entity.vo.PaginationResultVO;
import com.scoremg.entity.vo.ResponseVO;

import com.scoremg.exception.BusinessException;
import com.scoremg.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.Multipart;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

import com.scoremg.service.CourseSelectInfoService;

@RestController
@RequestMapping("/score")
public class CourseSelectInfoController extends ABaseController {

	@Resource
	private CourseSelectInfoService courseSelectInfoService;

	@Resource
	private UserInfoService userInfoService;


	/**
	 * 分页查询
	 */
	@RequestMapping("/loadScoreList")
	@GlobalInterceptor()
	public LayuiResultVO loadDataList(HttpSession session, @RequestBody LayuiSearchQuery<CourseSelectInfoQuery> query) {
		WebSessionUserDto sessionUserDto = getUserInfoFromSession(session);
		UserInfo dbUserInfo = userInfoService.getUserInfoByUserId(sessionUserDto.getUserId());
		CourseSelectInfoQuery infoQuery = new CourseSelectInfoQuery();
		if (sessionUserDto.getRoleType().equals(RoleTypeEnums.TEACHER.getType())) {
			infoQuery.setQueryTeacherInfo(true);
			infoQuery.setTeacherId(dbUserInfo.getStuJobNo());
		}

		if (sessionUserDto.getRoleType().equals(RoleTypeEnums.STUDENT.getType())) {
			infoQuery.setQueryStudentInfo(true);
			infoQuery.setStuJobNo(dbUserInfo.getStuJobNo());
		}

		infoQuery.setPageNo(query.getPage());
		infoQuery.setPageSize(query.getLimit());
		BeanUtils.copyProperties(query.getData(), infoQuery);
		PaginationResultVO<CourseSelectInfo> listByPage = courseSelectInfoService.findListByPage(infoQuery);
		LayuiResultVO<UserInfo> layuiResultVO = getSuccessLayuiResultVO(listByPage.getList());
		layuiResultVO.setCount(listByPage.getTotalCount());
		return layuiResultVO;
	}

	/**
	 * 保存分数信息
	 * @param session
	 * @param id 学生表 id
	 * @param score 分数
	 * @return
	 */
	@RequestMapping("/saveScore")
	@GlobalInterceptor()
	public ResponseVO saveScore(HttpSession session,@NotNull Integer id,@NotNull BigDecimal score) {
		CourseSelectInfo updateInfo = new CourseSelectInfo();
		updateInfo.setGrade(score);
		// 定义 100 作为比较基准
		BigDecimal hundred = new BigDecimal("100");
		// 比较 score 和 100
		int comparisonResult = score.compareTo(hundred);
		if (comparisonResult > 0) {
			throw new BusinessException("成绩不能超过100");
		}
		// 定义 0 作为比较基准
		BigDecimal zero = BigDecimal.ZERO;
		// 比较 score 和 0
		int comparisonResult1 = score.compareTo(zero);
		if (comparisonResult1 <0) {
			throw new BusinessException("成绩不能为负数");
		}
		courseSelectInfoService.updateCourseSelectInfoById(updateInfo,id);
		return getSuccessResponseVO(null);
	}


	/**
	 * 批量导入成绩信息
	 * @param file excel 成绩表格
	 * @return
	 */
	@RequestMapping("/importData")
	@GlobalInterceptor()
	public ResponseVO saveScore(Multipart file) {
		return getSuccessResponseVO(null);
	}

}